const app = getApp()
const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}
const formatDate = date => {
  date || (date = new Date())
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  return [year, month, day].map(formatNumber).join('-')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

const getMonthDays = () => {
  let date = new Date()
  let year = date.getFullYear()
  let mouth = date.getMonth() + 1
  let days
  if (mouth == 2) {
    days = year % 4 == 0 ? 29 : 28;
  } else if (mouth == 1 || mouth == 3 || mouth == 5 || mouth == 7 || mouth == 8 || mouth == 10 || mouth == 12) {
    //月份为：1,3,5,7,8,10,12 时，为大月.则天数为31；
    days = 31;
  } else {
    //其他月份，天数为：30.
    days = 30;
  }
  return days;
}
//验证手机号
const isMobile = (s) => {
  return /^1[0-9]{10}$/.test(s)
}
//验证银行卡号

const isBankCard = (s) => {
  return /^([1-9]{1})(\d{15}|\d{18})$/.test(s)
}
//验证是否是农业银行卡
const isAbcBankCard = (s) => {
  return /^(622841|622824|622826|622848|620059|621282|622828|622823|621336|621619|622821|622822|622825|622827|622845|622849|623018|623206|621671|622840|622843|622844|622846|622847|620501)\d{13}$/g.test(s)
}
const formatTimeDiff = date => {
  let now = new Date().getTime()
  let monthDays = 30
  date = date.replace(/\-/g, '/')
  date = new Date(date).getTime()
  let diff = (now - date) / 1000
  if (diff < 60) {
    return '刚刚'
  }
  if (diff >= 60 && diff < 3600) {
    return ~~(diff / 60) + '分钟前'
  }
  if (diff >= 3600 && diff < 86400) {
    return ~~(diff / 3600) + '小时前'
  }
  if (diff >= 86400 && diff < monthDays * 86400) {
    return ~~(diff / 86400) + '天前'
  }
  if (diff >= monthDays * 86400 && diff < monthDays * 86400 * 12) {
    return ~~(diff / (monthDays * 86400)) + '月前'
  }
  return ~~(diff / (monthDays * 86400 * 12)) + '年前'
}
const formatTimeDiff1 = date => {
  let now = new Date().getTime()
  let monthDays = 30
  date = date.replace(/\-/g, '/')
  date = new Date(date).getTime()
  let diff = (now - date) / 1000
  if (diff < 60) {
    return '刚刚发布'
  }
  if (diff >= 60 && diff < 3600) {
    return ~~(diff / 60) + '分钟前发布'
  }
  if (diff >= 3600 && diff < 86400) {
    return ~~(diff / 3600) + '小时前发布'
  }
  if (diff >= 86400 && diff < monthDays * 86400) {
    return ~~(diff / 86400) + '天前发布'
  }
  if (diff >= monthDays * 86400 && diff < monthDays * 86400 * 12) {
    return ~~(diff / (monthDays * 86400)) + '月前发布'
  }
  return ~~(diff / (monthDays * 86400 * 12)) + '年前发布'
}
/**
 * 设置缓存默认时间30天
 *
 * @param key
 * @param data
 * @param time 时间单位：秒
 */
const setStorage = (key, data, time) => {
  let date = new Date();
  let time1 = date.getTime();
  if (time) {
    time = time1 + (time * 1000 * 60);
  } else {
    time = time1 + (60 * 1000 * 60 * 24 * 30);
  }

  let info = {
    data: data,
    over_time: time
  };
  wx.setStorageSync('fly_' + key, info);
}

/**
 * 取缓存
 * @param key
 * @returns {string}
 */
const getStorage = key => {
  let info = wx.getStorageSync('fly_' + key);
  let date = new Date();
  let time1 = date.getTime();
  return (!info || time1 > info['over_time']) ? '' : info['data'];
}

/**
 * 弹窗提醒
 * @param msg
 * @returns {boolean}
 */
const alert = msg => {
  if (!msg) return false;
  wx.showToast({
    title: msg,
    icon: 'none',
    mask: true,
    duration: 2000
  });
}

/**
 * 字符串转json
 * @param s
 * @returns {object}
 */
const stringToJson = s => {
  return JSON.parse(s);
}

/**
 * json转字符串
 * @param o
 * @returns {string}
 */
const jsonToString = o => {
  return JSON.stringify(o)
}

const setToken = (token) => {
  setStorage('token', token)
}

const getToken = () => {
  return getStorage('token')
  // return '704759429941166080'
}

const setRealNameAuth = (data) => {
  setStorage('realname_auth', data)
}

const RealnameAuth = () => {
  return getStorage('realname_auth')
}
const checkRealNameLoading = () => {
  let info = RealnameAuth()
  if (!info) return 4
  info = info[0]
  return info.realStatus
}
const checkRealName = () => {
  let info = RealnameAuth()
  if (!info) return false
  info = info[0]
  if (info.realStatus == 2 || info.realStatus == '2') return true
  return false
}

const needRealName = (msg = '请完成实名认证') => {
  wx.showModal({
    title: msg,
    showCancel: false,
    content: '',
    success: function () {
      wx.navigateTo({
        url: '/pages/layout/mine/verified/index'
      })
    }
  });
}
const needRealName2 = (msg = '根据国家法律法规，以及本次大赛要求，请先完成实名认证，再上传参赛作品，实名认证1-2个工作日完成') => {
  wx.showModal({
    title: msg,
    showCancel: false,
    content: '',
    success: function () {
      wx.navigateTo({
        url: '/pages/layout/mine/verified/index'
      })
    }
  });
}
const needRealNameLoading = (msg = '请等待实名认证审核成功') => {
  wx.showModal({
    title: msg,
    showCancel: false,
    content: '',
    success: function () {
      wx.navigateTo({
        url: '/pages/layout/mine/verified/index'
      })
    }
  });
}
const needBindingPhone = (msg = '请先绑定手机号') => {
  wx.showModal({
    title: msg,
    showCancel: false,
    content: '',
    success: function () {
      wx.navigateTo({
        url: '/pages/login/bindingPhone/index'
      })
    }
  });
}

/**
 * 登录页面
 * @param msg
 */
const needLogin = (msg = '身份已过期，请重新登录') => {
  wx.showModal({
    title: msg,
    showCancel: false,
    content: '',
    success: function () {
      wx.clearStorageSync();
      wx.navigateTo({
        url: '/pages/login/login'
      })
    }
  });
}


const checkLogin = () => {
  let token = getToken()
  if (token) return true;
  return false
}
const fileHost = "xxxx.aliyuncs.com(你的阿里云oss地址)"
const config = {
  //aliyun OSS config
  uploadImageUrl: `${fileHost}`, //默认存在根目录，可根据需求改
  AccessKeySecret: '填你自己的AccessKeySecret',
  OSSAccessKeyId: '填你自己的 OSSAccessKeyId',
  timeout: 87600 //这个是上传文件时Policy的失效时间
};
module.exports = {
  formatTime,
  needRealNameLoading,
  needBindingPhone,
  formatTimeDiff,
  formatDate,
  setStorage,
  getStorage,
  alert,
  stringToJson,
  jsonToString,
  needLogin,
  checkLogin,
  setToken,
  getToken,
  isMobile,
  isBankCard,
  isAbcBankCard,
  setRealNameAuth,
  RealnameAuth,
  checkRealName,
  needRealName,
  formatTimeDiff1,
  checkRealNameLoading,
  needRealName2
}